package interactor;

import context.*;
	
import mainFrame.DrawPanel;

import java.awt.Graphics2D;
import java.awt.image.ImageObserver;

import object2D.PathObject;

import org.json.JSONException;

import util.PixelPoint;

// ********************************************************************
// *** CLASS: DrawPathInteractor
// ********************************************************************
public class DrawPathInteractor extends Interactor implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="interactor.DrawPathInteractor";

  public String getMouseModeMnemonic() { return("Draw Path"); }
  private PathObject polygon; // !null when drawing a Path
  private PixelPoint prev;

  // ******************************************************************
  // *** CONSTRUCTOR: DrawPathInteractor
  // ******************************************************************
  public DrawPathInteractor(DrawPanel drawPanel) { super(drawPanel,DRAW_PATH); }

  // ******************************************************************
  // *** METHOD: paintMouseEffect
  // ******************************************************************
  public void paintMouseEffect(Graphics2D g,ImageObserver imageObserver)
  { if(polygon!=null) polygon.paint(g,imageObserver); }

  // ******************************************************************
  // *** METHOD: mouseDragged
  // ******************************************************************
  // Invoked when a mouse button is pressed and then dragged.
  protected void mouseDragged()
  {	if(polygon!=null)
	{ 
	  double dx=Math.abs(mouseCurrent.getX()-prev.getX());
	  double dy=Math.abs(mouseCurrent.getY()-prev.getY());
	  if((dx>5) || (dy>5))
	  { PixelPoint p=mouseCurrent.copy();
	    polygon.add(p); prev=p;
	    drawPanel.repaint();
	  }
	}
  }

  // ******************************************************************
  // *** METHOD: mouseMoved
  // ******************************************************************
  // Invoked when the mouse has been moved (with no buttons down).
  protected void mouseMoved() {}

  // ******************************************************************
  // *** METHOD: mousePressed
  // ******************************************************************
  // Invoked when a mouse button has been pressed.
  protected void mouseLeftPressed(PixelPoint p)
  {	if(polygon==null)
    { try { polygon=new PathObject(drawPanel.getObjectModel(),p); }
	  catch(JSONException e) { e.printStackTrace(); }
      prev=p.copy();   
    }
  }
  protected void mouseRightPressed(PixelPoint p) {}

  // ******************************************************************
  // *** METHOD: mouseReleased
  // ******************************************************************
  // Invoked when a mouse button has been released.
  protected void mouseLeftReleased(PixelPoint p)
  { if(polygon!=null)
	{ if(polygon.numberOfPoints()>2) drawObjectDone(polygon,"PATH");
	  polygon=null;
	}
  }
  protected void mouseRightReleased(PixelPoint p) {}

}